package com.wechat.android.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.wechat.android.app.R;
import com.wechat.android.app.callback.RecyclerViewItemSlideHelper;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseRecyclerAdapter<T, VH extends BaseRecyclerAdapter.ViewHolder> extends RecyclerView.Adapter<VH> implements RecyclerViewItemSlideHelper.Callback{
    public Context mContext;
    public List<T> data = new ArrayList<>();
    public final LayoutInflater layoutInflater;
    protected OnItemClickListener onItemClickListener;
    protected OnItemLongClickListener onItemLongClickListener;
    protected OnItemDeleteListener onItemDeleteListener;
    protected RecyclerView mRecyclerView;
    protected boolean mSlideEnabled = true;
    protected boolean mReboundEnabled = true;

    public BaseRecyclerAdapter(Context context, List<T> data ) {
        this(context,data,false,false);
    }
    public BaseRecyclerAdapter(Context context, List<T> data , boolean slideEnabled , boolean reboundEnabled){
        this.mContext = context;
        this.data.addAll(data);
        this.layoutInflater = LayoutInflater.from(context);
        this.mSlideEnabled = slideEnabled;
        this.mReboundEnabled = reboundEnabled;
    }
    public void setList(List<T> list){
        data.clear();
        appendList(list);
    }

    public void appendList(List<T> list){
        data.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if(data == null){
            return 0;
        }else {
            return data.size();
        }

    }

    @Override
    public void onBindViewHolder(final VH holder, final int position) {
        if (onItemClickListener != null && holder.itemView != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(position,data.get(position),holder.itemView );
                }
            });
        }
        if (onItemLongClickListener != null && holder.itemView != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return onItemLongClickListener.onItemLongClick(position, data.get(position), holder.itemView );
                }
            });
        }

    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onViewRecycled(VH holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRecyclerView = recyclerView;
        if(mSlideEnabled){
            mRecyclerView.addOnItemTouchListener(new RecyclerViewItemSlideHelper(mRecyclerView.getContext(), this));
        }
    }

    @Override
    public int getHorizontalRange(RecyclerView.ViewHolder holder) {
        View view  =  holder.itemView.findViewById(R.id.slide_button_layout);
        if(view != null){
            return holder.itemView.findViewById(R.id.slide_button_layout).getLayoutParams().width;
        }
        /*if(holder.itemView instanceof LinearLayout){
            ViewGroup viewGroup = (ViewGroup) holder.itemView;
            if(viewGroup.getChildCount() == 2){
                return viewGroup.getChildAt(1).getLayoutParams().width;
            }
        }*/
        return 0;
    }
    @Override
    public RecyclerView.ViewHolder getChildViewHolder(View childView) {
        return mRecyclerView.getChildViewHolder(childView);
    }
    @Override
    public View findTargetView(float x, float y) {
        return mRecyclerView.findChildViewUnder(x, y);
    }
    public interface OnItemClickListener<T> {
        void onItemClick(int position, T t, View view);
    }

    public interface OnItemLongClickListener<T> {
        boolean onItemLongClick(int position, T t, View view);
    }
    public interface OnItemDeleteListener<T>{
        void onItemDelete(int position, T t, View view);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.onItemLongClickListener = listener;
    }
    public void setOnItemDeleteListener(OnItemDeleteListener listener){
        this.onItemDeleteListener = listener;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
